import { _decorator, Component, Animation, Collider2D, Contact2DType, Node, Vec3, v3, Label, game } from 'cc';
import { Constant } from '../AshFramework/Datas/Constant';
import { Terminus } from './Terminus';
import { Monster } from './Monster';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { GameManager } from './GameManager';
import { BattleManager } from './BattleManager';
const { ccclass, property } = _decorator;

enum AniState {
    Idle = "Role_Idle",
    Attack = "Role_Attack",
}

@ccclass('Hero')
export class Hero extends Component {
    public static Instance: Hero = null;
    @property
    CD: number = 2;
    @property
    Harm: number = 10;

    Animation: Animation = null;
    HPLabel: Label = null;

    private _hp: number = 0;
    private _state: AniState = AniState.Idle;
    private _isPlaying: boolean = false;
    private _cb: Function = null;

    private _arrMonster: Array<Node> = new Array();
    private _target: Node = null;
    private _terminus: Node = null;
    private _timer: number = 0;

    protected onLoad(): void {
        Hero.Instance = this;

        this.Animation = this.getComponent(Animation);
        this.HPLabel = this.node.getChildByName("HP").getComponent(Label);
        this._hp = Math.floor(GameManager.Instance.userData.hp * (1 + GameManager.Instance.userData.propertyAmplification.ROLE_HP / 100));//属性赋值
        this.showHP();

        const collider: Collider2D = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }

        this.Animation.on(Animation.EventType.FINISHED, this.onAnimationFinished, this);
    }

    protected start(): void {
        BattleManager.Instance.addBattleValue(this.Harm);
    }

    showHP(num: number = 0) {
        this._hp += num;
        this.HPLabel.string = String(this._hp);
    }

    protected update(dt: number): void {
        this.node.position = v3(60, 60)
        this._timer += dt;
        if (this.getTarget() && this._timer >= this.CD && !this._isPlaying) {
            this._timer = 0;
            this.attack();
        }
    }

    //根据碰撞将怪兽添加到对滴数组里面
    onBeginContact(self: Collider2D, other: Collider2D) {
        if (other.group === Constant.Group.monster && other.tag == 0) {
            if (!this._target) {
                this._target = other.node;
            }
            this._arrMonster.push(other.node);
        }
        if (other.group === Constant.Group.terminus && other.tag == 0 && !this._terminus) {
            this._terminus = other.node;
        }
    }

    getTarget(): Node | null {
        if (this._target && this._target.isValid) return this._target;
        if (this._terminus) return this._terminus;

        for (let i = 0; i < this._arrMonster.length; i++) {
            if (this._arrMonster[i].isValid) {
                return this._arrMonster[i];
            }
        }
        return null;
    }
    attack() {
        if (this._terminus) {
            this.PlayAni(AniState.Attack, () => {
                if (this._target && this._target.isValid) {
                    this._target.getComponent(Terminus).beHit(this.Harm);
                }
                this.PlayAni(AniState.Idle);
            })
        } else {
            this.PlayAni(AniState.Attack, () => {
                this._target = this.getTarget();
                if (this._target && this._target.isValid) {
                    this._target.getComponent(Monster).beHit(this.Harm);
                }
                this.PlayAni(AniState.Idle);
            })
        }
    }
    PlayAni(state: AniState, cb: Function = null) {
        if (this._state == state || this._isPlaying) {
            return
        }
        this._state = state;
        if (state === AniState.Attack) {
            this._isPlaying = true;
        }
        this._cb = cb;
        this.Animation.crossFade(state);
    }

    onAnimationFinished() {
        this._isPlaying = false;
        this._cb && this._cb();
        this._cb = null;
    }

    removeMonster(monster: Node) {
        if (!monster || !monster.isValid) return;
        if (this._target === monster) {
            this._target = null;
        }
        for (let i = 0; i < this._arrMonster.length; i++) {
            if (this._arrMonster[i] === monster) {
                this._arrMonster.splice(i, 1);
                return;
            }
        }

    }

    beHit(harm: number) {
        this.showHP(-harm);
        if (this._hp <= 0) {
            console.error(`游戏结束！！`);
        }
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.REMOVE_MONSTER, this.removeMonster, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.REMOVE_MONSTER, this.removeMonster, this);
    }

}


